﻿using Authority.DataTransferObject.Subsystem;
using Authority.IServices;
using Authority.IServices.Model.Subsystem;
using Authority.PresentationModels.Subsystem;
using AutoMapper;
using HelperFromTheSkeleton.APICore;
using HelperFromTheSkeleton.Model.PageModel;
using HelperFromTheSkeleton.Model.RequestModels;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using System.ComponentModel.DataAnnotations;

namespace Authority.WebAPI.Controllers
{
    /// <summary>
    /// 子系统控制器
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    [Authorize]
    public class SubsystemController : BaseController
    {

        private readonly ISubsystemService _subsystemService;
        private readonly IMapper _mapper;

        /// <summary>
        /// 构造
        /// </summary>
        /// <param name="subsystemService"></param>
        /// <param name="mapper"></param>
        public SubsystemController(ISubsystemService subsystemService, IMapper mapper)
        {
            _subsystemService = subsystemService;
            _mapper = mapper;
        }
        /// <summary>
        /// 新增子系统
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public async Task<ResultModel> Add(AddSubsystemRequestModel requestModel)
        {
            return await HandleAsync(async () =>
            {
                var model = _mapper.Map<AddSubsystemModel>(requestModel);
                await _subsystemService.AddAsync(model);
            });
        }

        /// <summary>
        /// 修改子系统
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public async Task<ResultModel> Edit(EditSubsystemRequestModel requestModel)
        {
            return await HandleAsync(async () =>
            {
                var model = _mapper.Map<EditSubsystemModel>(requestModel);
                await _subsystemService.EditAsync(model);
            });
        }

        /// <summary>
        /// 删除子系统
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<ResultModel> Delete([Required] Guid id)
        {
            return await HandleAsync(async () =>
            {
                await _subsystemService.DeleteAsync(id);
            });
        }

        /// <summary>
        /// 获取子系统详情
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public async Task<ResultModel<List<SubsystemDTO>>> Get([Required] List<Guid> ids)
        {
            return await HandleAsync(async () =>
            {
                List<SubsystemDTO> result = await _subsystemService.GetAsync(ids);
                return result;
            });
        }

        /// <summary>
        /// 获取子系统详情
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public async Task<PageResultModel<SubsystemListDTO>> Query(QuerySubsystemRequestModel requestModel)
        {
            return await HandleAsync(async () =>
            {
                var model = _mapper.Map<QuerySubsystemModel>(requestModel);
                return await _subsystemService.QueryAsync(model);
            });
        }

        /// <summary>
        /// 获取所有的子系统
        /// </summary>
        /// <returns></returns>
        [AllowAnonymous,HttpGet]
        public async Task<ResultModel<List<SubsystemListDTO>>> GetAllSubsystem()
        {
            return await HandleAsync(async () =>
            {
                return await _subsystemService.GetAllSubsystemAsync();
            });
        }

    }
}
